// pages/review/add.js
// 引入相关服务
const hospitalService = require('../../services/hospital');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    appointmentId: null,
    staffId: null,
    score: 0,
    content: '',
    canSubmit: false,
    scoreTexts: ['非常差', '差', '一般', '好', '非常好']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.appointmentId && options.staffId) {
      this.setData({
        appointmentId: options.appointmentId,
        staffId: options.staffId
      });
    } else {
      wx.showToast({
        title: '参数错误',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }
  },

  /**
   * 设置评分
   */
  setScore(e) {
    const score = parseInt(e.currentTarget.dataset.score);
    
    this.setData({
      score: score
    });
    
    this.checkCanSubmit();
  },

  /**
   * 内容输入事件
   */
  onContentInput(e) {
    this.setData({
      content: e.detail.value
    });
    
    this.checkCanSubmit();
  },

  /**
   * 检查是否可以提交
   */
  checkCanSubmit() {
    const { score, content } = this.data;
    
    this.setData({
      canSubmit: score > 0 && content.trim().length > 0
    });
  },

  /**
   * 提交评价
   */
  submitReview() {
    if (!this.data.canSubmit) {
      return;
    }
    
    // 显示加载中
    wx.showLoading({
      title: '提交中...',
      mask: true
    });
    
    // 构建提交的数据
    const reviewData = {
      staffId: this.data.staffId,
      appointmentId: this.data.appointmentId,
      score: this.data.score,
      content: this.data.content
    };
    
    // 调用添加评价API
    hospitalService.addReview(reviewData)
      .then(res => {
        wx.hideLoading();
        
        wx.showToast({
          title: '评价成功',
          icon: 'success',
          duration: 2000,
          success: function() {
            // 返回上一页
            setTimeout(() => {
              wx.navigateBack();
            }, 2000);
          }
        });
      })
      .catch(err => {
        console.error('评价失败', err);
        wx.hideLoading();
        
        wx.showToast({
          title: err.message || '评价失败，请重试',
          icon: 'none'
        });
      });
  }
}); 